﻿/*
 * KLF Headless Browser
 * Written by Kris Oye <kristianoye@gmail.com>
 * Copyright (C) 2011-2012.  All rights reserved.
 */
namespace KLF.Lib.Browser.DOM.CSS
{
    #region Using Statements

    using KLF.Lib.Browser.DOM.Style;

    #endregion

    /// <summary>
    /// The CSSMediaRule interface represents a @media rule in a CSS style 
    /// sheet. A @media rule can be used to delimit style rules for 
    /// specific media types.
    /// </summary>
    public class CSSMediaRule : CSSRule
    {
        #region Constructors

        internal CSSMediaRule()
        {
            cssRules = new CSSRuleList(new CSSRule[0]);
            media = new MediaList();
        }

        #endregion

        #region DOM Level 2 CSS Compliance

        internal void AddRule(CSSRule rule)
        {
            cssRules.Add(rule);
        }

        /// <summary>
        /// A list of all CSS rules contained within the media block.
        /// </summary>
        public CSSRuleList cssRules
        {
            get;
            internal set;
        }

        /// <summary>
        /// Used to delete a rule from the media block. 
        /// </summary>
        /// <param name="index"></param>
        public void deleteRule(int index)
        {
            if (index > -1 && index < cssRules.length)
            {
                cssRules.RemoveAt(index);
            }
            throw new DOMException(DOMException.INDEX_SIZE_ERR);
        }

        /// <summary>
        /// Used to insert a new rule into the media block. 
        /// </summary>
        /// 
        /// <param name="rule">The parsable text representing the rule. For 
        /// rule sets this contains both the selector and the style 
        /// declaration. For at-rules, this specifies both the at-identifier
        /// and the rule content.</param>
        /// 
        /// <param name="index">The index within the media block's rule 
        /// collection of the rule before which to insert the specified rule.
        /// If the specified index is equal to the length of the media 
        /// blocks's rule collection, the rule will be added to the end of 
        /// the media block.</param>
        /// <returns></returns>
        public int insertRule(string rule, int index)
        {
            if (index == this.cssRules.length)
            {
                cssRules.Add(new CSSRule());
                return index;
            }
            else if (index < 0 || index > this.cssRules.length)
            {
                throw new DOMException(DOMException.INDEX_SIZE_ERR);
            }
            else
            {
                cssRules.Insert(index, new CSSRule());
                return index;
            }
        }

        /// <summary>
        /// A list of media types for this rule.
        /// </summary>
        public MediaList media
        {
            get;
            internal set;
        }

        #endregion
    }
}
